package org.loy.eclipse.post_debug_perspective;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.ui.progress.UIJob;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.WorkbenchException;

import org.loy.eclipse.post_debug_perspective.Activator;

public class SetPerspectiveJob extends UIJob
{
	public SetPerspectiveJob(String perspectiveId)
	{
		super("Set perspective: " + perspectiveId);
		this.perspectiveId = perspectiveId;
	}
	
	@Override
	public IStatus runInUIThread(IProgressMonitor monitor)
	{
		// TODO check this(maybe not currently active window must be used)
		IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();

		try {
			PlatformUI.getWorkbench().showPerspective(perspectiveId, window);
		} catch (WorkbenchException e) {
			return new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Unable to set perspective: " + perspectiveId, e);
		}
		
		return new Status(IStatus.OK, Activator.PLUGIN_ID, "Perspective: " + perspectiveId + " set", null);
	}

	private String perspectiveId;
}
